package cn.madis.ymcc.mq.consumer;


import cn.madis.ymcc.dto.Order2PayDto;
import cn.madis.ymcc.service.IPayOrderService;
import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@RocketMQMessageListener(
        consumerGroup = "service-course-order-consumer",//组名
        topic = "order-topic",
        selectorExpression = "payorder-tags",//tags
        messageModel = MessageModel.CLUSTERING//集群模式一个消费只能给到一个消费者
        //广播模式，一个消息可以被多个消费者消费
)
public class Order2PayConsumer implements RocketMQListener<MessageExt>{
    @Autowired
    private IPayOrderService payOrderService;


    @Override
    public void onMessage(MessageExt messageExt) {
        byte[] body = messageExt.getBody();
        String s = new String(body, StandardCharsets.UTF_8);
        Order2PayDto order2PayDto = JSONObject.parseObject(s, Order2PayDto.class);
        payOrderService.savaPayOrder(order2PayDto);

    }
}
